<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 行业分类
 */
namespace app\allwin\controller;
use app\allwin\model\AllwinEcard;
use app\allwin\widget\Card;
use app\allwin\model\AllwinStore;

class Ecard extends Common{

    public function initialize() {
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'惠卡配置','url'=>url("ecard/index")]]);
    }

    /**
     * 列表
     */
    public function index(){
        if(request()->isAjax()){
            $data = [
                'id'                => input('post.id/s'),
                'brand_name'        => input('post.brand_name/s'),
                'annual_fee'        => input('post.annual_fee/s'),
                'gh_id'             => input('post.gh_id/s'),
                'title'             => input('post.title/s'),
                'notice'            => input('post.notice/s'),
                'description'       => input('post.description/s'),
                'quantity'          => input('post.quantity/d'),
                'color'             => input('post.color/s'),
                'picture'           => input('post.picture/s'),
                'end_time'          => input('post.end_time/s'),
                'prerogative'       => input('post.prerogative/s'),
                'start_time'        => input('post.start_time/s'),
                'member_miniapp_id' => $this->member_miniapp_id
            ];
            $data['end_time'] = empty($data['end_time']) ? 0 : strtotime($data['end_time']);
            $data['start_time'] = empty($data['start_time']) ? 0 : strtotime($data['start_time']);
            $validate = $this->validate($data,'Ecard.edit');
            if(true !== $validate){
                return enjson(0,$validate);
            }
            if(empty($data['id'])){
                $data['create_time'] = time();
                $rel = Card::create($data);
                if($rel['errcode'] == 0){
                    $data['card_id'] = $rel['card_id'];
                    $result =  AllwinEcard::create($data);
                }else{
                    return enjson(0,'操作失败');
                }
            }else{
                $info = AllwinEcard::where(['member_miniapp_id' => $this->member_miniapp_id, 'id' => $data['id']])->find();
                if($info->start_time == $data['start_time'] && $info->end_time == $data['end_time']){
                    $state  = 0;
                }else if($info->start_time > $data['start_time'] && $info->end_time < $data['end_time']){
                    $state  = 1;
                }else{
                    return enjson(0,'只支持往后延长时间');
                }
                //增加减少库存
                $rel['errcode'] = 0;
                if($info->quantity > $data['quantity']){
                    $rel = Card::reductStock($info['card_id'],$info->quantity - $data['quantity'], $this->member_miniapp_id);
                }else if($info->quantity < $data['quantity']){
                    $rel = Card::increaseStock($info['card_id'],$data['quantity'] - $info->quantity, $this->member_miniapp_id);
                }
                if($rel['errcode'] == 0){
                    $rel = Card::update($data,$info['card_id'],$state);
                    if($rel['errcode'] == 0){
                        $result =  AllwinEcard::where(['id' =>  $data['id']])->update($data);
                    }else{
                        return enjson(0,'操作失败');
                    }
                }else{
                    return enjson(0,'操作失败');
                }
            }
            if($result){
                return enjson(200,'操作成功',['url'=>url('ecard/index')]);
            }else{
                return enjson(0,'操作失败');
            }
        }else{
            $rel          = AllwinEcard::where(['member_miniapp_id' => $this->member_miniapp_id])->find();
            empty($rel['card_id']) ? $view['card'] = [] : $view['card'] = Card::info($rel['card_id'],$this->member_miniapp_id);
            $view['info'] = $rel;
            return view()->assign($view);
        }
    }

    /**
     * 已绑定会员卡列表
     */
    public function storeIndex(int $id){
        $info = AllwinEcard::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $id])->find();
        $view['lists']   = AllwinStore::where(['member_miniapp_id' => $this->member_miniapp_id])->whereIn('id',$info->ids)->order('id desc')->paginate(20);
        $view['ecard_id'] = $info->id;
        return view()->assign($view);
    }


    /**
     * 好店列表
     */
    public function select(int $ecard_id){
        $condition   = [];
        $condition[] = ['member_miniapp_id', '=', $this->member_miniapp_id];
        $info = AllwinEcard::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $ecard_id])->find();
        if(request()->isAjax()){
            $ids     = $this->request->param('ids');
            if(empty($ids)){
                return enjson(0,'请选择要添加会员卡的好店');
            }
            if(!empty($info->ids)){
                $ids = $info->ids . ',' . substr($ids,0,strlen($ids) - 1);
            }else{
                $ids = substr($ids,0,strlen($ids) - 1);
            }
            $result = AllwinEcard::where($condition)->where(['id' => $ecard_id])->update(['ids' => $ids]);
            if($result){
                return enjson(200,'好店添加会员卡成功');
            }else{
                return enjson(0,'好店添加会员卡失败');
            }
        }else{
            $keyword     = $this->request->param('keyword');
            if (!empty($keyword)) {
                $condition[] = ['name', 'like', '%' . $keyword . '%'];
            }
            $view['lists']   = AllwinStore::where($condition)->whereNotIn('id', $info->ids)->order('id desc')->paginate(20);
            $view['keyword'] = $keyword;
            $view['ecard_id'] = $ecard_id;
            return view()->assign($view);
        }
    }

    //删除
    public function delete(int $id){
        $info = AllwinEcard::where(['id' => $id])->find();
        $rel = Card::delete($info->card_id,$this->member_miniapp_id);
        if($rel['errcode'] == 0){
           $result =  AllwinEcard::destroy($id);
            if($result){
                return enjson(200,'删除成功');
            }else{
                return enjson(403,'删除失败');
            }
        }else{
            return enjson(403,'删除失败');
        }
    }

    //从会员卡中删除好店
    public function deleteStore(int $id,$ecard_id){
        $info      = AllwinEcard::where(['member_miniapp_id' => $this->member_miniapp_id, 'id' => $ecard_id])->find();
        $info->ids = ids(array_values_unset($id, ids($info->ids, true)));
        $result    = $info->save();
        if ($result) {
            return enjson(200, '删除成功');
        } else {
            return enjson(403, '删除失败');
        }
    }
}